package com.hundsun.documentripper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.dto.UserLogView;
import com.hundsun.documentripper.domain.pojo.UserLog;
import com.hundsun.documentripper.domain.query.LogQuery;
import com.hundsun.documentripper.mapper.UserLogMapper;
import com.hundsun.documentripper.service.UserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：日志接口实现
 * @date ：Created in 2021/1/29
 */
@Service
public class UserLogServiceImpl implements UserLogService {

    @Autowired
    private UserLogMapper userLogMapper;

    @Override
    public void insertUserLog(String userUuid, String productUuid, String productVersionUuid, String documentUuid, int documentVersion, String fileUuid) {
        userLogMapper.insert(UserLog.builder()
                .userUuid(userUuid)
                .productUuid(productUuid)
                .productVersionUuid(productVersionUuid)
                .documentUuid(documentUuid)
                .documentVersion(documentVersion)
                .fileUuid(fileUuid)
                .build());
    }

    @Override
    public IPage<UserLogView> getUserLogViewListByPage(LogQuery logQuery) {
        QueryWrapper<UserLogView> wrapper = getCommonQueryWrapper(logQuery);
        return userLogMapper.selectLogView(new Page<UserLogView>(logQuery.getCurrent(), logQuery.getSize()), wrapper);
    }

    @Override
    public IPage<UserLogView> getUserCurrentLogViewListByPage(LogQuery logQuery) {
        QueryWrapper<UserLogView> wrapper = getCommonQueryWrapper(logQuery);
        return userLogMapper.selectCurrentLogView(new Page<UserLogView>(logQuery.getCurrent(), logQuery.getSize()), wrapper);
    }

    /**
     * 获取常用的日志查询对象
     * @param logQuery
     * @return
     */
    private QueryWrapper<UserLogView> getCommonQueryWrapper(LogQuery logQuery) {
        QueryWrapper<UserLogView> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(logQuery.getUserUuid()), "l.user_uuid", logQuery.getUserUuid());
        wrapper.eq(!StringUtils.isEmpty(logQuery.getProductUuid()), "l.product_uuid", logQuery.getProductUuid());
        wrapper.eq(!StringUtils.isEmpty(logQuery.getProductVersionUuid()), "l.product_version_uuid", logQuery.getProductVersionUuid());
        wrapper.eq(!StringUtils.isEmpty(logQuery.getDocumentUuid()), "l.document_uuid", logQuery.getDocumentUuid());
        wrapper.eq(!StringUtils.isEmpty(logQuery.getDocumentVersion()), "l.document_version", logQuery.getDocumentVersion());
        wrapper.eq(!StringUtils.isEmpty(logQuery.getFileUuid()), "l.file_uuid", logQuery.getFileUuid());
        wrapper.orderByDesc("create_time");
        return wrapper;
    }

}